package com.hepinghospital.user.ui.kotlin

import android.annotation.SuppressLint
import android.graphics.pdf.PdfDocument
import android.view.View
import androidx.core.content.ContextCompat
import com.hepinghospital.user.R
import com.hepinghospital.user.base.BaseActivity
import com.hepinghospital.user.base.BaseApplication
import com.hepinghospital.user.eventbus.BookingRegisterSuccess
import com.hepinghospital.user.ui.dialog.ConfirmDialog
import com.hepinghospital.user.util.ToastUtil
import com.rest.business.RestProxy
import com.rest.observer.BaseObserver
import com.rest.response.DailyRecordDetailResponse
import com.rest.response.ScheduleResponse
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.activity_daily_detail.*
import kotlinx.android.synthetic.main.include_title.*
import org.greenrobot.eventbus.EventBus

/**
 * @Description: 预约挂号——订单详情
 * @Author: lzp
 * @CreateDate: 2023/3/29 9:16
 * @UpdateRemark: 更新说明
 */
class DailyDetailActivity : BaseActivity() {

    private lateinit var outpId: String

    override fun getLayoutResource(): Int = R.layout.activity_daily_detail

    override fun initViews() {
        tv_title.text = "订单详情"
        btn_cancel.visibility = View.GONE
        btn_appoint.visibility = View.GONE
        btn_cancel.setOnClickListener {
            showConfirmDialog(outpId)
        }
        btn_appoint.setOnClickListener {
            startActivity(DailyRegisterActivity::class.java)
            finish()
        }
        btn_back.setOnClickListener {
            finish()
            EventBus.getDefault().post(BookingRegisterSuccess())
        }
    }

    override fun initData() {
        outpId = intent.getStringExtra("outpId").toString()
        getDailyDetail()
    }

    private fun showConfirmDialog(outpId: String) {
        val dialog = ConfirmDialog.newInstance("确定取消预约吗？","")
        dialog.show(supportFragmentManager, "ConfirmDialog")
        dialog.setmListener(object : ConfirmDialog.OnItemClickListener {
            override fun onConfirm() {
                cancelDaily(outpId)
            }

            override fun onCancel() {
            }
        })
    }

    override fun onBackPressed() {
        super.onBackPressed()
        EventBus.getDefault().post(BookingRegisterSuccess())
    }

    private fun cancelDaily(outpId: String) {
        RestProxy.getInstance().cancelDaily(outpId, object : Observer<String> {
            override fun onComplete() {
            }

            override fun onSubscribe(d: Disposable) {
            }

            override fun onNext(t: String) {
                getDailyDetail()
            }

            override fun onError(e: Throwable) {
                ToastUtil.onError(BaseApplication.context, e)
            }
        })
    }

    private fun getDailyDetail() {
        RestProxy.getInstance()
            .getDailyDetail(outpId, object : BaseObserver<DailyRecordDetailResponse>() {
                @SuppressLint("SetTextI18n")
                override fun onSuccess(t: DailyRecordDetailResponse) {
                    tv_name2.text = t.docName
                    tv_patient.text = t.patientName
                    tv_deptName2.text = t.deptName
                    tv_date.text = t.scheduleDate
                    tv_price.text = "${t.regFee} 元"
                    tv_time.text = t.scheduleTime
                    when (t.flag) {
                        401 -> {
                            img_status.setImageResource(R.drawable.ic_success)
                            tv_status.text = "预约成功"
                            tv_message.text = "咨询已预约，请及时就诊～"
                            btn_cancel.visibility = View.VISIBLE
                            scl_header.shapeDrawableBuilder.setSolidGradientColors(ContextCompat.getColor(mContext, R.color.white), ContextCompat.getColor(mContext, R.color.color_B3F6F8FD), ContextCompat.getColor(mContext, R.color.color_FFE2ECFF))
                                .intoBackground()
                        }
                        402 -> {
                            img_status.setImageResource(R.drawable.ic_cancel)
                            tv_status.text = "取消预约"
                            tv_message.text = "咨询已取消，欢迎再次咨询～"
                            btn_cancel.visibility = View.GONE
                            btn_appoint.visibility = View.VISIBLE
                            scl_header.shapeDrawableBuilder.setSolidGradientColors(ContextCompat.getColor(mContext, R.color.color_FFF9F9F9), ContextCompat.getColor(mContext, R.color.color_B3F6F8FD), ContextCompat.getColor(mContext, R.color.color_FFE4E4E4))
                                .intoBackground()
                        }
                        403 -> {
                            img_status.setImageResource(R.drawable.ic_fail)
                            tv_status.text = "预约失败"
                            tv_message.text = "预约失败，请重新预约～"
                            btn_cancel.visibility = View.GONE
                            btn_appoint.visibility = View.VISIBLE
                            scl_header.shapeDrawableBuilder.setSolidGradientColors(ContextCompat.getColor(mContext, R.color.white), ContextCompat.getColor(mContext, R.color.color_B3F6F8FD), ContextCompat.getColor(mContext, R.color.color_FFE2ECFF))
                                .intoBackground()
                        }
                    }
                }

                override fun onError(e: Throwable) {
                    super.onError(e)
                    ToastUtil.onError(mContext, e)
                }
            })
    }
}